#!/bin/bash
#this script adds or removes an article from excluded entries list
#you can use this feature, for example, if you wrote custom HTML in some articles and you don't want nci to destroy your changes.
if [ ! -d "entries" ] || [ ! -d "public_html" ]; then
        echo "You haven't initialized nci system yet... Please use \"./init\" before posting a new entry."
        exit 1
fi

#check if the excluded entries file exists
if [ ! -f "exclude.conf" ]; then
	echo "The exclude.conf file doesn't exists... please create it or run the \"./init\" command again (it won't affect your existing files)"
fi

#check if no id entered.
if [ -z "$1" ]; then
        echo "Error: Please type the ID of the entry you want to exclude/include from being updated when you run \"./refresh\" (Usage: \"./exclude <entry_id>\")."
        exit 1
fi

if [ "$1" == "-c" ]; then
	truncate -s 0 "exclude.conf"
	echo "All excluded entries were removed!"
	exit 1
fi

if [ "$1" == "-a" ]; then
	entry_count=$(ls "entries/" | wc -l)
	((entry_count-=1))
	for (( i=1; i<=$entry_count; i++))
	do
		echo $i >> "exclude.conf"
	done
	echo "All entries are now excluded!"
	exit 1
fi

lnum=0
while read -r line; do
	((lnum+=1))
	if [ "$1"  == "$line" ]; then
		echo "Entry removed from the list of excluded entries. Now it'll be updated the next time you run the \"./refresh\" command."
		sed -i $lnum"d" "exclude.conf"
		exit 1
	fi
done < "exclude.conf"

echo "$1" >> "exclude.conf"
echo "Entry added to the list of excluded entries. Now it won't be updated when you run the \"./refresh\" command, so you need to modify it manually from the HTML source."
echo "Protip: You can remove an entry from the list of excludes by running the \"./exclude $1\" command again."
